package cz.muni.fi.pa165.sme.web.panels.sportActivityRecord;

import java.util.Date;

import cz.muni.fi.pa165.sme.api.SportActivityRecordService;
import cz.muni.fi.pa165.sme.api.SportActivityService;
import cz.muni.fi.pa165.sme.api.UserService;
import cz.muni.fi.pa165.sme.api.to.SportActivityRecordTO;
import cz.muni.fi.pa165.sme.web.common.ErrorBehavior;
import cz.muni.fi.pa165.sme.web.converters.ConvertUtils;
import cz.muni.fi.pa165.sme.web.fo.SportActivityFO;
import cz.muni.fi.pa165.sme.web.fo.SportActivityRecordFO;
import cz.muni.fi.pa165.sme.web.fo.UserFO;
import cz.muni.fi.pa165.sme.web.pages.sportActivityRecord.RecordListPage;
import org.apache.wicket.extensions.yui.calendar.DateTimeField;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.NumberTextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.visit.IVisit;
import org.apache.wicket.util.visit.IVisitor;
import org.apache.wicket.validation.validator.MinimumValidator;
import org.joda.time.DateTime;
import org.springframework.core.convert.ConversionService;

/**
 * @author jirankova
 */
public class RecordForm extends Panel {

	@SpringBean
	private SportActivityService sportActivityService;

	@SpringBean
	private SportActivityRecordService sportActivityRecordService;

	@SpringBean
	private UserService userService;

	@SpringBean(name = "conversionServiceFe")
	private ConversionService conversionService;

	/**
	 * Form to manipulate with {@link SportActivityRecordFO}
	 * @param id panel id
	 * @param model form model
	 */
	public RecordForm(final String id, final IModel<SportActivityRecordFO> model) {
		super(id, model);

		//sport activity - choice
		final FormComponent<SportActivityFO> sportActivities = new DropDownChoice<SportActivityFO>("sportActivity",
				ConvertUtils.convertList(sportActivityService.findAllSportActivities(), conversionService,
						SportActivityFO.class), new ChoiceRenderer<SportActivityFO>("title", "id"));
		sportActivities.setRequired(true);

		//user - choice
		final FormComponent<UserFO> users = new DropDownChoice<UserFO>("user",
				ConvertUtils.convertList(userService.findAllUserTO(), conversionService,
						UserFO.class), new ChoiceRenderer<UserFO>("lastName", "id"));
		users.setRequired(true);

		final FormComponent<Date> activityStart = new DateTimeField("activityStart");
		activityStart.setRequired(true);

		final FormComponent<Float> distance = new NumberTextField<Float>("distance").add(new MinimumValidator<Float>(0f));

		final FormComponent<Float> duration = new NumberTextField<Float>("duration").add(new MinimumValidator<Float>(0f));
		duration.setRequired(true);

		final Form<SportActivityRecordFO> form = new Form<SportActivityRecordFO>("recordForm", model) {
			@Override
			protected void onSubmit() {
				super.onSubmit();
				final SportActivityRecordFO activityRecordFO = model.getObject();

				final SportActivityRecordTO activityRecordTO = conversionService.convert(activityRecordFO, SportActivityRecordTO.class);

				//if duration is set, count activity stop time
				if(activityRecordFO.getDuration() != null) {
					activityRecordTO.setActivityStop(new DateTime(activityRecordFO.getActivityStart()));
					final int minutes = Math.round(activityRecordFO.getDuration() * 360);
					activityRecordTO.getActivityStop().plusMinutes(minutes);
				}

				if(sportActivityRecordService.exists(activityRecordTO)) {
					sportActivityRecordService.updateSportActivityRecord(activityRecordTO);
				} else {
					sportActivityRecordService.createSportActivityRecord(activityRecordTO);
				}
				setResponsePage(RecordListPage.class);
			}
		};

		form.add(sportActivities);
		form.add(users);
		form.add(activityStart);
		form.add(distance);
		form.add(duration);
		add(form);

		form.visitChildren(FormComponent.class, new IVisitor<FormComponent<?>, IVisit<Void>>() {
			@Override
			public void component(final FormComponent<?> object, final IVisit<IVisit<Void>> visit) {
				object.add(new ErrorBehavior());
				visit.dontGoDeeper();
			}
		});

		final FeedbackPanel feedbackPanel = new FeedbackPanel("feedbackPanel");
		feedbackPanel.setOutputMarkupId(true);
		add(feedbackPanel);
	}

}
